﻿using System;
using System.Windows;
using PDO.DataPersistence.Core;
using PDO.DataPersistence.ServiceProvider;

namespace NorthwindApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            IDataObjectSourceProvider dataProvider = new DataObjectSourceDirect("PDO.DataConnection");

            if (!dataProvider.TestConnection())
            {
                System.Windows.MessageBox.Show("Database connection test failed.  Application exiting.");
                App.Current.Shutdown();
            }

            DataSession.SetDataSourceProvider(dataProvider);
        }

        public App()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
        }

     

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            DisplayException(e.Exception, false);
            e.Handled = true;
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            DisplayException((Exception)e.ExceptionObject, e.IsTerminating);
        }

        public static void DisplayException(Exception ex, bool isTerminating)
        {
            MessageBox.Show(ex.ToString(), "Error");
        }
    }
}
